// 从 URL 获取书籍 ID
const params = new URLSearchParams(window.location.search);
const bookId = params.get('id');

// 加载书籍内容
async function loadBookContent() {
    try {
        if (!bookId) {
            throw new Error('未指定书籍ID');
        }

        console.log('正在加载书籍:', bookId);
        const response = await fetch(`../admin/api/books/get.php?id=${bookId}`);
        const result = await response.json();
        
        console.log('API返回结果:', result);
        
        if (!result.success) {
            throw new Error(result.message || '加载失败');
        }
        
        if (!result.data || !result.data.contentImages) {
            throw new Error('书籍数据格式错误');
        }

        // 设置全局变量供其他 JS 使用
        window.BOOK_CONTENT = {
            title: result.data.title,
            pages: result.data.contentImages,
            totalPages: result.data.contentImages.length,
            updatedAt: new Date().toISOString()
        };

        console.log('设置BOOK_CONTENT:', window.BOOK_CONTENT);

        // 更新配置
        if (window.bookConfig) {
            bookConfig.totalPageCount = window.BOOK_CONTENT.totalPages;
            bookConfig.bookTitle = result.data.title;
            
            // 设置资源文件路径
            bookConfig.assets = {
                preloader: "style/preloader.gif",
                transparent: "files/transparent.png",
                coverImg: result.data.cover || "files/thumb/1.jpg"
            };
            
            console.log('更新bookConfig:', bookConfig);
        }

    } catch (error) {
        console.error('加载失败详细信息:', error);
        alert('加载失败: ' + error.message);
    }
}

// 初始加载
loadBookContent(); 